package com.javatiaocao.myblog.service.impl;

import com.javatiaocao.myblog.constant.CodeType;
import com.javatiaocao.myblog.mapper.TagMapper;
import com.javatiaocao.myblog.model.Tags;
import com.javatiaocao.myblog.service.TagService;
import com.javatiaocao.myblog.utils.DataMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class TagServiceImpl implements TagService {
    @Autowired
    private TagMapper tagMapper;

    @Override
    public void insertTags(String[] newArticleTags, int tagSize) {
        for (String newArticleTag : newArticleTags) {
            //判断,防止重复
            if (!tagMapper.findIsExistByTagName(newArticleTag)){
                Tags tag = new Tags(newArticleTag,tagSize);
                tagMapper.saveTags(tag);
            }

        }
    }


    /**
     * findTagsCloud
     * @return
     */
    @Override
    public DataMap findTagsCloud() {
        HashMap<String, Object> returnObject = new HashMap<>();
        List<Tags> tagsList = tagMapper.findTagsCloud();
        if(tagsList == null || tagsList.size() == 0){
            return DataMap.fail(CodeType.SERVER_EXCEPTION);
        }
        ArrayList<Tags> tags = new ArrayList<>();
        for (Tags tag : tagsList) {
            HashMap<String, Object> object = new HashMap<>();
            object.put("tagName", tag.getTagName());
            object.put("tagSize", tag.getTagSize());
            tags.add(tag);
        }
        returnObject.put("result", tags);

        return DataMap.success(CodeType.FIND_TAGS_CLOUD).setData(returnObject);

    }
}
